//
//  ViewController.m
//  nxt.仿SD框架
//
//  Created by 倪啸天 on 16/10/15.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic, strong) NSOperationQueue *queue;

@end

@implementation ViewController

- (NSOperationQueue *)queue
{
    if (!_queue)
    {
        _queue = [NSOperationQueue new];
    }
    return _queue;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageView.backgroundColor = [UIColor blueColor];
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [self downloadAndSetIMGWithURL:@"http://img1.3lian.com/img013/v3/56/d/101.jpg"];
    
}

- (void)downloadAndSetIMGWithURL:(NSString *)urlString
{
    
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        
        NSURL *url = [NSURL URLWithString:urlString];
        
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        UIImage *image = [UIImage imageWithData:data];
        
        NSLog(@"加载OK.");
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
          
            _imageView.image = image;
            
            NSLog(@"OK");
            
        }];
        
    }];
    
    [self.queue addOperation:op];
    
}

@end
